package com.uplooking.pms.system.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.uplooking.pms.base.controller.BaseController;
import com.uplooking.pms.common.util.CommonUtil;
import com.uplooking.pms.common.vo.DwzPageBean;
import com.uplooking.pms.system.entity.Student;
import com.uplooking.pms.system.entity.User;
import com.uplooking.pms.system.service.IStudentService;


/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author uplooking
 * @since 2017-12-01
 */
@Controller
@RequestMapping("system/student")
public class StudentController extends BaseController {

	@Autowired
	private IStudentService studentService;


	@RequestMapping("/list")
	public String list( DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request,
			HttpServletResponse response) {

	/*	dwzPageBean.getCountResultMap().put("name", name);
		dwzPageBean.getCountResultMap().put("phone", phone);*/

		/*Wrapper wrapper = Condition.create().like("name", name).like("phone", phone);*/

		Page resultPage = studentService.selectPage(dwzPageBean.toPage());
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));

		return "system/student/list";
	}

	@RequestMapping("/toInsert")
	public String toInsert(HttpServletRequest request, HttpServletResponse response) {

		return "system/student/insert";
	}
	
	@RequestMapping("/insert")
	@ResponseBody
	public Object insert(Student student,HttpServletRequest request, HttpServletResponse response) {
		Wrapper<Student> wrapper = Condition.create().eq("name", student.getName());
		List<Object> list= studentService.selectObjs(wrapper);
		//List<Student> list=studentService.findByName(student.getName());
		if (list!=null&&list.size()>0) {
			return ajaxError("姓名已重复，重新命名");
		}

		if (studentService.insert(student)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
		
	}

/*	@RequestMapping("/insert")
	@ResponseBody
	public Object insert(User user, HttpServletRequest request, HttpServletResponse response) {
		Wrapper<User> wrapper = Condition.create().eq("name", user.getName()).or().eq("phone", user.getPhone());
		User user2 = studentService.selectOne(wrapper);
		if (!CommonUtil.isBlank(user2)) {
			return ajaxError("姓名或手机重复");
		}

		if (studentService.insert(user)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}*/

	@RequestMapping("/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", studentService.selectById(id));
		return "system/student/insert";
	}

	@RequestMapping("/update")
	@ResponseBody
	public Object update(Student student, HttpServletRequest request, HttpServletResponse response) {
		if (studentService.updateAllColumnById(student)) {
			return ajaxSuccess("更改成功");
		} else {
			return ajaxError();
		}
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object update(String[] id, HttpServletRequest request, HttpServletResponse response) {
		if (studentService.deleteBatchIds(Arrays.asList(id))) {
			return ajaxSuccess("删除成功");
		} else {
			return ajaxError();
		}
	}

}
